package com.example.zyfapplication.utils;

import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.RectF;
import android.media.FaceDetector;

import com.example.baeshelplibrary.utils.LogUtil;
import com.example.zyfapplication.base.BaseApplication;

import java.util.ArrayList;
import java.util.List;

// 这个方类就是为了用来发现人脸，以及给人脸绘制框框的
public class FindAndDrowFaceUtils {

    private static FindAndDrowFaceUtils instace;
    private static final int MAX_FACE = 5;
    private static Long nowTime = 0L;
    private static Long oldTime = 0L;
    public static haveFindFace findFace;
    private static List<RectF> mFace = new ArrayList<>();


    private static FindAndDrowFaceUtils getInstance() {
        if (instace == null) {
            synchronized (FindAndDrowFaceUtils.class) {
                if (instace == null) {
                    instace = new FindAndDrowFaceUtils();

                }
            }
        }
        return instace;
    }

    public static void findFaceFromBitmap(final Bitmap bitmap) {


        nowTime = TimeUtil.getLocationTimeLong();
        if (nowTime - oldTime > 1000) {
            oldTime = nowTime;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    Bitmap checkBit = bitmap.copy(Bitmap.Config.RGB_565, true);
                    FaceDetector faceDetector = new FaceDetector(checkBit.getWidth(), checkBit.getHeight(), MAX_FACE);
                    FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACE];
                    int realFaceNum = faceDetector.findFaces(checkBit, faces);
                    if (realFaceNum > 0) {
                        findFace.isFinish();
                        findFace.haveFace(realFaceNum);
                    } else {
                        findFace.isFinish();
                    }

                    //获取人脸区域
                    float eyesDistance = 0f;
                    mFace.clear();
                    for (int i = 0; i < realFaceNum; i++) {
                        PointF eyeMidPoint = new PointF();//两眼的中点距离
                        faces[i].getMidPoint(eyeMidPoint);//两眼之间的距离

                        eyesDistance = faces[i].eyesDistance();
                        float distanceX = 1.6f * eyesDistance;
                        float distanceTop = 1.2f * eyesDistance;
                        float distanceBottom = 2.0f * eyesDistance;
                        float left = eyeMidPoint.x - distanceX - 10;
                        float right = eyeMidPoint.x + distanceX;
                        float top = eyeMidPoint.y - distanceTop;
                        float bottom = eyeMidPoint.y + distanceBottom;
                        RectF rectF = new RectF();
                        rectF.set(left, top, right, bottom);
                        mFace.add(rectF);

                    }
                    findFace.ShowFace(mFace);

                }
            };

            ThreadManager.getThreadPool().execute(runnable);
        }

    }

    public interface haveFindFace {
        void haveFace(int number);
        void ShowFace(List<RectF> faces);
        void isFinish();
    }

    public static void SetHaveFindFaceListener(haveFindFace listener) {
        findFace = listener;
    }
}
